/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean.inspector.renderer;

import com.jb.web.application.NWConstants;
import com.jb.web.bean.FormField;
import com.jb.web.bean.inspector.PropertyInspector;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.RenderKitImpl;
import com.jb.web.renderer.RendererImpl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jb.faces.component.UIComponent;


public class FormRenderer extends RendererImpl {
    static {
        RenderKitImpl.registerRenderer(PropertyInspector.class, FormRenderer.class);
    }

    public StringBuffer renderBegin(WebContext pCtx, UIComponent itemToRender) {
         return renderFormHeader(pCtx, itemToRender);
    }
    
    private StringBuffer renderFormHeader(WebContext pCtx, UIComponent itemToRender) {
        PropertyInspector form = (PropertyInspector) itemToRender;
        StringBuffer html = new StringBuffer("");
        String action = pCtx.formURL(form.getAction(), form);

        action = this.evaluate(pCtx, form, action);

        html.append("<FORM name=\"f").append(form.getClientId()).append("\"")
            .append(addTagProperty("action", action, "")).append(" method=\"POST\">");
        html.append("<TABLE ").append(addTagProperty("cellSpacing", "0px", null))
            .append(addTagProperty("cellPadding", "0px", null))
            .append(addTagProperty("class", form.getStyleClass(), null)).append(addTagProperty("width", "100%", null))
            .append(" >");
        return html;
    }

    public StringBuffer renderChildren(WebContext pCtx, UIComponent itemToRender) {
        PropertyInspector form = (PropertyInspector) itemToRender;
        //List list = form.getChildrenByType(FormField.class);
        Map categories =form.getCategories();
        Iterator iter = categories.values().iterator();
        StringBuffer html = new StringBuffer("");
        while(iter.hasNext())
        {
            PropertyInspector.Category category=(PropertyInspector.Category)iter.next();
            html.append("<tr><td><div class=\"jb_g_title\" onclick=\"Inspector.toggleGroup('"+category.name+"')\" >")
                .append(category.name).append("</div><div id=\"ip"+category.name+"\" style=\"display:"
                    + (category.visible?"block":"none")+"\"><table width=\"100%\">");
                
                html.append(rendererGroup(pCtx, category.name, itemToRender, category.items));
                
            html.append("</table></div></td></tr>");
        }
        return html;
    }
    
    private StringBuffer rendererGroup(WebContext pCtx, String category, UIComponent itemToRender, List list)
    {
        StringBuffer html = new StringBuffer("");
        int n = 0;
        
        while (n < list.size()) {
            html.append("<TR >");

            FormField hi = (FormField) list.get(n++);

            if (isVisible(pCtx, hi)) {
                html.append(hi.render(pCtx));
            } else {
                html.append("<input type=\"hidden\" name=\"").append(hi.getClientId()).append("\" value=\"")
                    .append(evaluate(pCtx, hi, hi.getValue())).append("\">");
            }

            html.append("</TR>\n");
        }
        return html;
    }

    public StringBuffer renderEnd(WebContext pCtx, UIComponent itemToRender) {
        PropertyInspector form = (PropertyInspector) itemToRender;
        StringBuffer html = new StringBuffer();
        html.append("<TR><TD align=\"center\" colspan=").append(2).append(">");

        if (form.showDefaultButton()) {
            html.append("<input type=\"submit\" class=\"jbButton\" value=\"Sumbit\" name=\"submit\">").append("&nbsp;");
        }

        html.append("<input type=\"hidden\" name=\"" + NWConstants.NW_EVENT_SOURCE + "\"  value=\"")
            .append(form.getQName()).append("\" />").append("</TD></TR></TABLE></FORM>");

        return html;
    }
}
